package com.company.card_service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.card_service.mapper.IUserInfoMapper;
import com.company.card_service.mapper.PassageMapper;
import com.company.card_service.mapper.PassageViewMapper;
import com.company.card_service.model.CompanyCaseType;
import com.company.card_service.mapper.CompanyCaseTypeMapper;
import com.company.card_service.model.Passage;
import com.company.card_service.model.PassageView;
import com.company.card_service.model.User;
import com.company.card_service.service.CompanyCaseTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.card_service.vo.CompanyCaseTypeVo;
import com.company.card_service.vo.CompanyTypeVo;
import com.company.card_service.vo.PassageAndView;
import com.company.card_service.vo.PassageFromVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 赵志浩
 * @since 2022-09-15 04:15:06
 */
@Service
public class CompanyCaseTypeServiceImpl extends ServiceImpl<CompanyCaseTypeMapper, CompanyCaseType> implements CompanyCaseTypeService {

    @Autowired
    private CompanyCaseTypeMapper companyCaseTypeMapper;

    @Autowired
    private IUserInfoMapper iUserInfoMapper;

    @Autowired
    private PassageViewMapper passageViewMapper;

    @Autowired
    private PassageMapper passageMapper;

    @Override
    public List<CompanyTypeVo> getAllDataOfCompanyTypeAndPassage(Integer id){
        return companyCaseTypeMapper.getAllDataOfCompanyTypeAndPassage(id);
    }

    @Override
    public PassageAndView getPassageDetails(Integer id) {
        return companyCaseTypeMapper.getPassageDetails(id);
    }

    @Override
    public List<CompanyCaseTypeVo> getCompanyCaseTypeInfo(Integer page,Integer limit,Integer sysUserId,Integer modelId) {
        Page<CompanyCaseTypeVo> pageParam = new Page<>(page,limit);
        List<CompanyCaseTypeVo> companyCaseTypeInfo = companyCaseTypeMapper.getCompanyCaseTypeInfo(pageParam,sysUserId,modelId);
        List<CompanyCaseTypeVo> collect = companyCaseTypeInfo.stream().sorted(Comparator.comparing(CompanyCaseTypeVo::getSort)).collect(Collectors.toList());
        return collect;
    }

    /**
     * 下移
     * @param sysUserId
     * @param modelId
     * @param caseTypeId
     */
    @Override
    public void findNext(Integer sysUserId, Integer modelId, Integer caseTypeId) {
        //查询当前公司 对应模块的分类
        List<CompanyCaseType> next = companyCaseTypeMapper.findNext(sysUserId, modelId);
        //根据sort排序
        List<CompanyCaseType> collect = next.stream().sorted(Comparator.comparing(CompanyCaseType::getSort)).collect(Collectors.toList());
        //下移
        this.selectNext(collect,caseTypeId);
    }

    /**
     * 下移的方法
     * @param collect
     * @param caseTypeId
     */
    private void selectNext(List<CompanyCaseType> collect, Integer caseTypeId) {
        //记录下标
        int xia = 0;
        //根据caseTypeId再集合中找到 当前caseTypeId 的下标 并记录
        for (int i = 0; i < collect.size(); i++) {
            if(collect.get(i).getId().equals(caseTypeId)){
                xia = i;
            }
        }
        //根据记录的下标 找到对应的CompanyCaseType
        CompanyCaseType currentEl = collect.get(xia);
        //记录的下标加一 就是下一个元素的下标 ，然后找到 下一个下标对应的CompanyCaseType
        CompanyCaseType nextEl = collect.get(xia + 1);
        //分别获取 他们的sort
        Integer currentElCompanyId = currentEl.getSort();
        Integer nextElCompanyId = nextEl.getSort();
        //交换他们的sort
        nextEl.setSort(currentElCompanyId);
        currentEl.setSort(nextElCompanyId);
        //修改数据
        companyCaseTypeMapper.updateById(currentEl);
        companyCaseTypeMapper.updateById(nextEl);
    }

    /**
     * 上移
     * @param sysUserId
     * @param modelId
     * @param caseTypeId
     */
    @Override
    public void findPre(Integer sysUserId, Integer modelId, Integer caseTypeId) {
        //查询当前公司 对应模块的分类
        List<CompanyCaseType> next = companyCaseTypeMapper.findPre(sysUserId, modelId);
        //根据sort排序
        List<CompanyCaseType> collect = next.stream().sorted(Comparator.comparing(CompanyCaseType::getSort)).collect(Collectors.toList());
        //上移
        this.selectPre(collect,caseTypeId);
    }

    @Override
    public void addCaseTpye(CompanyCaseType companyCaseType) {
        LambdaQueryWrapper<CompanyCaseType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompanyCaseType::getCompanyId,companyCaseType.getCompanyId());
        wrapper.eq(CompanyCaseType::getModelId,companyCaseType.getModelId());
        List<CompanyCaseType> companyCaseTypes = baseMapper.selectList(wrapper);
        Optional<CompanyCaseType> max = companyCaseTypes.stream().max(Comparator.comparing(CompanyCaseType::getSort));
        CompanyCaseType caseType = max.get();
        Integer maxSort = caseType.getSort();
        companyCaseType.setSort(maxSort+1);
        baseMapper.insert(companyCaseType);
    }

    /**
     * 上移的方法
     * @param collect
     * @param caseTypeId
     */

    private void selectPre(List<CompanyCaseType> collect, Integer caseTypeId) {
        //记录下标
        int xia = 0;
        //根据caseTypeId再集合中找到 当前caseTypeId 的下标 并记录
        for (int i = 0; i < collect.size(); i++) {
            if(collect.get(i).getId().equals(caseTypeId)){
                xia = i;
            }
        }
        //根据记录的下标 找到对应的CompanyCaseType
        CompanyCaseType currentEl = collect.get(xia);
        //记录的下标减一 就是上一个元素的下标 ，然后找到 上一个下标对应的CompanyCaseType
        CompanyCaseType nextEl = collect.get(xia - 1);
        //分别获取 他们的sort
        Integer currentElCompanyId = currentEl.getSort();
        Integer nextElCompanyId = nextEl.getSort();
        //交换他们的sort
        nextEl.setSort(currentElCompanyId);
        currentEl.setSort(nextElCompanyId);
        //修改数据
        companyCaseTypeMapper.updateById(currentEl);
        companyCaseTypeMapper.updateById(nextEl);
    }

    @Override
    public CompanyCaseType getCaseType(Integer caseTypeId) {
        CompanyCaseType companyCaseType = baseMapper.selectById(caseTypeId);
        return companyCaseType;
    }

    @Override
    public List<PassageFromVo> getAllPassage(Integer page,Integer limit,Integer sysUserId) {
        Page<PassageFromVo> pageParam = new Page<>(page,limit);
        List<PassageFromVo> allPassage = companyCaseTypeMapper.getAllPassage(pageParam,sysUserId);
        allPassage.stream().forEach(s -> this.getSysUserNameAndId(s));
        return allPassage;
    }

    private PassageFromVo getSysUserNameAndId(PassageFromVo s) {
        User user = iUserInfoMapper.selectById(s.getSysUserId());
        LambdaQueryWrapper<PassageView> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PassageView::getPassageId,s.getId());
        Long aLong = passageViewMapper.selectCount(wrapper);
        if(user != null){
            s.setRealView(aLong);
            s.setSysUserName(user.getName());
            s.setSysUserId(Integer.parseInt(user.getId().toString()));
        }

        return s;
    }

    @Override
    public List<CompanyCaseType> getCaseTypeName(Integer companyId) {
        List<CompanyCaseType> companyCaseTypes =  companyCaseTypeMapper.getCaseTypeName(companyId);
        return companyCaseTypes;
    }

    //获取公司案例类型(备选方案二)
    @Override
    public List<CompanyCaseType> getCompanyType(Integer id) {
        List<CompanyCaseType> companyType = companyCaseTypeMapper.getCompanyType(id);
        List<CompanyCaseType> collect = companyType.stream().sorted(Comparator.comparing(CompanyCaseType::getSort)).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<PassageAndView> getPassageByTypeId(Integer id) {
        List<PassageAndView> passageByTypeId = passageMapper.getPassageByTypeId(id);
        passageByTypeId.stream().forEach(item -> this.getRealView(item));
        return passageByTypeId;
    }

    private PassageAndView getRealView(PassageAndView item) {
        LambdaQueryWrapper<PassageView> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PassageView::getPassageId,item.getId());
        Long count = passageViewMapper.selectCount(wrapper);
        Integer baseView = item.getBaseView();
        item.setViewCount(count.intValue() + baseView);
        return item;
    }

    @Override
    public PassageAndView getPassageDetail(Integer id) {
        PassageAndView passageDetail = passageMapper.getPassageDetail(id);
        LambdaQueryWrapper<PassageView> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PassageView::getId,passageDetail.getId());
        Long count = passageViewMapper.selectCount(wrapper);
        passageDetail.setViewCount(count.intValue() + passageDetail.getBaseView());
        return passageDetail;
    }
}
